1   /*
2    * Copyright (C) 2007 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect;
18  
19  import static com.google.common.base.Preconditions.checkNotNull;
20  
21  import com.google.common.collect.testing.SetTestSuiteBuilder;
22  import com.google.common.collect.testing.TestStringSetGenerator;
23  import com.google.common.collect.testing.features.CollectionFeature;
24  import com.google.common.collect.testing.features.CollectionSize;
25  
26  import junit.framework.Test;
27  import junit.framework.TestCase;
28  
29  import java.io.Serializable;
30  import java.util.Collection;
31  import java.util.Collections;
32  import java.util.HashSet;
33  import java.util.Set;
34  
35  import javax.annotation.Nullable;
36  
37  /**
38   * Tests for {@code Synchronized#set}.
39   *
40   * @author Mike Bostock
41   */
42  public class SynchronizedSetTest extends TestCase {
43    
44    public static final Object MUTEX = new Integer(1); // something Serializable
45    
46    public static Test suite() {
47      return SetTestSuiteBuilder.using(new TestStringSetGenerator() {
48            @Override
49            protected Set<String> create(String[] elements) {
50              TestSet<String> inner = new TestSet<String>(new HashSet<String>(), MUTEX);
51              Set<String> outer = Synchronized.set(inner, inner.mutex);
52              Collections.addAll(outer, elements);
53              return outer;
54            }
55          })
56          .named("Synchronized.set")
57          .withFeatures(CollectionFeature.GENERAL_PURPOSE,
58              CollectionFeature.ALLOWS_NULL_VALUES,
59              CollectionSize.ANY,
60              CollectionFeature.SERIALIZABLE)
61          .createTestSuite();
62    }
63  
64    static class TestSet<E> extends ForwardingSet<E> implements Serializable {
65      final Set<E> delegate;
66      public final Object mutex;
67  
68      public TestSet(Set<E> delegate, Object mutex) {
69        checkNotNull(mutex);
70        this.delegate = delegate;
71        this.mutex = mutex;
72      }
73  
74      @Override protected Set<E> delegate() {
75        return delegate;
76      }
77  
78      @Override public String toString() {
79        assertTrue(Thread.holdsLock(mutex));
80        return super.toString();
81      }
82  
83      @Override public boolean equals(@Nullable Object o) {
84        assertTrue(Thread.holdsLock(mutex));
85        return super.equals(o);
86      }
87  
88      @Override public int hashCode() {
89        assertTrue(Thread.holdsLock(mutex));
90        return super.hashCode();
91      }
92  
93      @Override public boolean add(@Nullable E o) {
94        assertTrue(Thread.holdsLock(mutex));
95        return super.add(o);
96      }
97  
98      @Override public boolean addAll(Collection<? extends E> c) {
99        assertTrue(Thread.holdsLock(mutex));
100       return super.addAll(c);
101     }
102 
103     @Override public void clear() {
104       assertTrue(Thread.holdsLock(mutex));
105       super.clear();
106     }
107 
108     @Override public boolean contains(@Nullable Object o) {
109       assertTrue(Thread.holdsLock(mutex));
110       return super.contains(o);
111     }
112 
113     @Override public boolean containsAll(Collection<?> c) {
114       assertTrue(Thread.holdsLock(mutex));
115       return super.containsAll(c);
116     }
117 
118     @Override public boolean isEmpty() {
119       assertTrue(Thread.holdsLock(mutex));
120       return super.isEmpty();
121     }
122 
123     /* Don't test iterator(); it may or may not hold the mutex. */
124 
125     @Override public boolean remove(@Nullable Object o) {
126       assertTrue(Thread.holdsLock(mutex));
127       return super.remove(o);
128     }
129 
130     @Override public boolean removeAll(Collection<?> c) {
131       assertTrue(Thread.holdsLock(mutex));
132       return super.removeAll(c);
133     }
134 
135     @Override public boolean retainAll(Collection<?> c) {
136       assertTrue(Thread.holdsLock(mutex));
137       return super.retainAll(c);
138     }
139 
140     @Override public int size() {
141       assertTrue(Thread.holdsLock(mutex));
142       return super.size();
143     }
144 
145     @Override public Object[] toArray() {
146       assertTrue(Thread.holdsLock(mutex));
147       return super.toArray();
148     }
149 
150     @Override public <T> T[] toArray(T[] a) {
151       assertTrue(Thread.holdsLock(mutex));
152       return super.toArray(a);
153     }
154 
155     private static final long serialVersionUID = 0;
156   }
157 }